﻿global using Microsoft.AspNetCore.Html;

namespace WXWorkSvc.Common.TagHelpers;
[HtmlTargetElement("wx-input-items", TagStructure = TagStructure.WithoutEndTag)]
public class WxInputItemsTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    private readonly IHtmlGenerator _HtmlGenerator;

    public WxInputItemsTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }

    [HtmlAttributeName("asp-Items")]
    public ModelExpression Items { get; set; } = default!;
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-hidden-")]
    public Dictionary<string, ModelExpression> Hiddens { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression> Fors { get; set; } = new();

    [HtmlAttributeName(DictionaryAttributePrefix = "asp-readonly-")]
    public Dictionary<string, string> ReadOnlys { get; set; } = new();
    [HtmlAttributeName("asp-group")]
    public ModelExpression GroupFor { get; set; } = default!;
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {

        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("name", "自动创建");

        var tbValidationSummary = _HtmlGenerator.GenerateValidationSummary(ViewContext);
        if (tbValidationSummary != null)
            output.Content.AppendHtml(tbValidationSummary);


        var items = (IList)Items.Model;
        for (int i = 0; i < items.Count; i++)
        {
            var itemModelExplorer = Items.ModelExplorer.GetExplorerForModel(items[i]);
            if (GroupFor != null)
            {
                var p = itemModelExplorer.GetExplorerForProperty(GroupFor.Metadata.Name);
                var expression = new ModelExpression($"{Items.Name}[{i}].{GroupFor.Metadata.Name}", p);

                output.Content.AppendHtml($"""
                    <div class="weui-cells__title">{expression.Model}</div>

                    """);
            }
            var tagGroup = new TagBuilder("div");
            tagGroup.AddCssClass("weui-cells weui-cells_form");
            output.Content.AppendHtml(tagGroup);

            foreach (var f in Hiddens.Values)
            {
                var p = itemModelExplorer.GetExplorerForProperty(f.Metadata.Name);
                var expression = new ModelExpression($"{Items.Name}[{i}].{f.Metadata.Name}", p);
                var hiddenTag = _HtmlGenerator.GenerateHidden(ViewContext, expression.ModelExplorer, expression.Name, p.Model, false, null);
                tagGroup.InnerHtml.AppendHtml(hiddenTag);
            }
            foreach (var kv in Fors)
            {
                var f = kv.Value;
                var tagLabel = f.GenerateLabel("weui-label");

                f.GetHtmlAttributes(out var htmlAttributes, out var format);

                var p = itemModelExplorer.GetExplorerForProperty(f.Metadata.Name);
                var expression = new ModelExpression($"{Items.Name}[{i}].{f.Metadata.Name}", p);


                var tagInput = _HtmlGenerator.GenerateTextBox(ViewContext, expression.ModelExplorer, expression.Name, expression.Model, format, htmlAttributes);
                if (ReadOnlys.ContainsKey(kv.Key))
                {
                    tagInput.Attributes.Add("readonly", "readonly");
                    tagInput.Attributes.Add("class", "weui-input readonly");
                    tagInput.MergeAttribute("type", "text", true);
                    tagInput.Attributes.Remove("data-val");
                    tagInput.Attributes.Remove("data-val-required");
                }
                else
                    tagInput.Attributes.Add("class", "weui-input");
                tagGroup.InnerHtml.AppendHtml($"""
                                            <div class="weui-cell">
                                                <div class="weui-cell__hd">
                                                    {tagLabel.GetHtmlString()}
                                                </div>
                                                <div class="weui-cell__bd">
                                                    {tagInput.GetHtmlString()}
                                                </div>
                                            </div>
                                            """);

            }

        }



    }
}

